package com.recipedb.recipedb.service.impl;

import com.recipedb.recipedb.entity.RecipeComment;
import com.recipedb.recipedb.repository.RecipeCommentRepository;
import com.recipedb.recipedb.service.RecipeCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class RecipeCommentServiceImpl implements RecipeCommentService {

    @Autowired
    private RecipeCommentRepository repository;

    @Override
    public RecipeComment save(RecipeComment entity) {
        return repository.save(entity);
    }

    @Override
    public void deleteById(Long id) {
        repository.deleteById(id);
    }

    @Override
    public Optional<RecipeComment> findById(Long id) {
        return repository.findById(id);
    }

    @Override
    public List<RecipeComment> findAll() {
        return repository.findAll();
    }

    // 实现根据食谱ID查询评论的方法
    @Override
    public List<RecipeComment> findByRecipeId(Long recipeId) {
        // 调用Repository中定义的方法，按创建时间倒序返回
        return repository.findByRecipeIdOrderByCreatedAtDesc(recipeId);
    }

}
